package com.agriculture.data_demo.repository;

import com.agriculture.data_demo.entity.sys.BroilerBatchEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * @author morningbear
 * @date 20.10.7
 */
@Repository
public interface BroilerBatchRepository extends JpaRepository<BroilerBatchEntity, Integer> {

    @Query(value = "select * from broiler_batch", nativeQuery = true)
    Page<BroilerBatchEntity> findAll(PageRequest of);

    /**
     *
     * @param of 在执行查询方法时，可以传入一个PageRequest对象，代表进行分页查询。
     * PageRequest对象代表了查询的条件和约束，通常包含当前页数，每页几条数据。
     * 也可以在分页查询时指定Direction或Sort。
     * @return 去除重复的鸡舍
     */
    @Query(value = "select b from BroilerBatchEntity b group by b.fence")
    Page<BroilerBatchEntity> findDuplicated(PageRequest of);

    /**
     *
     * @param fence 鸡舍
     * @return 根据鸡舍查询
     */
    @Query(value = "select * from broiler_batch where fence = ?1", nativeQuery = true)
    Page<BroilerBatchEntity> findBroilerBatchEntitiesByFence(String fence, Pageable pageable);

    /**
     *
     * @param startDate 查询起始日期
     * @param endDate 查询结束日期
     * @return 查询时间段中的肉鸡生产批次数据
     */
    @Query(value = "select * from broiler_batch where broiler_batch.create_at between ?1 and ?2", nativeQuery = true)
    Page<BroilerBatchEntity> findBroilerBatchEntitiesByDate(Date startDate, Date endDate, Pageable pageable);
}
